/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package t20240516_lineage;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.flink.table.catalog.ObjectIdentifier;


@ToString
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
@Getter
@Setter
public class LineageRel {

    private String sourceCatalog;

    private String sourceDatabase;

    private String sourceTable;

    private String sourceColumn;

    private String targetCatalog;

    private String targetDatabase;

    private String targetTable;

    private String targetColumn;

    /**
     * Stores the expression for data conversion,
     * which source table fields are transformed by which expression the target field
     */
    private String transform;

    private static String DELIMITER = ".";



    public static LineageRel build(
            String sourceTablePath,
            String sourceColumn,
            String targetTablePath,
            String targetColumn,
            String transform) {
        String[] sourceItems = sourceTablePath.split("\\.");
        String[] targetItems = targetTablePath.split("\\.");

        return new LineageRel(
                sourceItems[0],
                sourceItems[1],
                sourceItems[2],
                sourceColumn,
                targetItems[0],
                targetItems[1],
                targetItems[2],
                targetColumn,
                transform);
    }
//
//    public static List<LineageRel> build(String catalog, String database, String[][] expectedArray) {
//        return Stream.of(expectedArray)
//                .map(e -> {
//                    LineageRel lineageRel = new LineageRel(catalog, database, e[0], e[1], e[2], e[3]);
//                    // transform field is optional
//                    if (e.length == 5) {
//                        lineageRel.setTransform(e[4]);
//                    }
//                    return lineageRel;
//                })
//                .collect(Collectors.toList());
//    }


    public String getSourceTablePath() {
        return sourceCatalog + DELIMITER + sourceDatabase + DELIMITER + sourceTable;
    }

    public String getTargetTablePath() {
        return targetCatalog + DELIMITER + targetDatabase + DELIMITER + targetTable;
    }


    public ObjectIdentifier getSourceTableIdentifier() {
        return ObjectIdentifier.of(sourceCatalog, sourceDatabase, sourceTable);
    }

    public ObjectIdentifier getTargetTableIdentifier() {
        return ObjectIdentifier.of(targetCatalog, targetDatabase, targetTable);
    }
}
